package com.yzt.zhuju;

import android.content.pm.PackageManager;
import android.text.TextUtils;

import com.alibaba.fastjson.JSONArray;
import com.yzt.zhuju.config.AppConfig;
import com.yzt.zhuju.config.AppConfigCacheManager;
import com.yzt.zhuju.network.entity.sysConfig.AppTextMapBean;
import com.yzt.p_base.utils.AppInfoUtils;

import java.util.List;

/**
 * author : bob
 * date   : 2022/11/30
 * desc   :
 */
public class MainMenuContsant {

    public static int mMainPageIndex = 0;

    public static final String MAIN_MENU_NEARBY = "menuNearby";          //附近
    public static final String MAIN_MENU_CHATROOM = "menuChatRoom";     //聊天室
    public static final String MAIN_MENU_MSG = "menuMsg";               //消息
    public static final String MAIN_MENU_MINE = "menuMine";             //我

    /**
     * 主页默认菜单
     * "menuSayu",
     */
    public static String DEFAULT_MAIN_MENU = "[\"menuNearby\",\"menuChatRoom\",\"menuMatch\",\"menuMsg\",\"menuMine\"]";

    public static List<String> getListMenu() {
        String mainMenu = AppConfigCacheManager.getInstance().getViewConfig(AppConfigCacheManager.HIDE_TYPE_MAIN_MENU);
        if (TextUtils.isEmpty(mainMenu) || TextUtils.equals("[]", mainMenu.replace(" ", ""))) {
            mainMenu = DEFAULT_MAIN_MENU;
        }

        return JSONArray.parseArray(mainMenu, String.class);
    }

    public static int getIndex(String menu) {
        List<String> listMenu = getListMenu();
        for (int i = 0; i < listMenu.size(); i++) {
            if (TextUtils.equals(listMenu.get(i), menu)) {
                return i;
            }
        }
        return 0;
    }

    /**
     * 是否有聊天室,使用此方法的地方都是做屏蔽的
     *
     * @return true 有  false 无
     */
    public static boolean isHaveChatRoom() {
        List<String> listMenu = MainMenuContsant.getListMenu();
        if (null != listMenu) {
            return listMenu.contains(MainMenuContsant.MAIN_MENU_CHATROOM);
        }
        return false;
    }

    public static boolean isLimitConsumption() {
        AppTextMapBean appTextMap = AppConfigCacheManager.getInstance().getAppTextMap();
        int versionCode;
        try {
            versionCode = AppConfig.context.getPackageManager().getPackageInfo(AppConfig.context.getPackageName(), 0).versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return false;
        }
        List<AppTextMapBean.LimitBean> limitBeans = appTextMap.getLimits();
        if (limitBeans == null) {
            return false;
        }
        for (int i = 0; i < limitBeans.size(); i++) {
            AppTextMapBean.LimitBean limitBean = limitBeans.get(i);
            if (limitBean.channel.equals(AppInfoUtils.getAppMetaData("UMENG_CHANNEL")) && limitBean.versionCode == versionCode) {
                return true;
            }
        }
        return false;
    }
}
